package com.zx.cashiersystem.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zx.cashiersystem.cashiersystem.R;
import com.zx.cashiersystem.entitys.OrderInfo;
import com.zx.cashiersystem.entitys.TableNumber;
import com.zx.cashiersystem.utils.Contants;

import java.util.List;

/**
 * Created by stone on 2016/12/22.
 */

public class OrderAdapter extends BaseAdapter {
    private Context context;
    private List<OrderInfo> list;
    private int select;

    public OrderAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        select = 0;
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.left_orderinfo_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.reset();
        if (select == i) {
            holder.check();
        }
        holder.tv_number.setText(i + 1+ "");
        holder.tv_order.setText("订单编号：" + list.get(i).getOrderid());
        holder.tv_time.setText(list.get(i).getOrderdate()/*.substring(11)*/);
        holder.tv_money.setText("￥"+list.get(i).getTotalprice());

        if ("tangchi".equals(list.get(i).getOrderfrom())){   //如果这个是堂吃
            holder.tv_orderinfo_left.setVisibility(View.VISIBLE);
            holder.iv_orderinfo_left.setVisibility(View.GONE);
            holder.tv_area1.setVisibility(View.VISIBLE);

            List<TableNumber> tableNumberList = JSON.parseObject(list.get(i).getTablenumber(),new TypeReference<List<TableNumber>>(){});
            if(tableNumberList.size() > 0){    //设置桌号,如果这个订单只做了一个桌,那就只显示一个桌号
                holder.tv_area1.setText(tableNumberList.get(0).getArea_name()+"区"+tableNumberList.get(0).getTable_name()+"桌");       //数据还没确定
            }
            if(tableNumberList.size() > 1){
                holder.tv_area2.setVisibility(View.VISIBLE);
                holder.tv_area2.setText(tableNumberList.get(1).getArea_name()+"区"+tableNumberList.get(1).getTable_name()+"桌");        //最多只设置两个桌号
            }


            if("0".equals(list.get(i).getOrderstatus())){          //订单状态,0是未出单,1是已出单
                holder.tv_orderinfo_left.setText("待");
                holder.tv_orderinfo_left.setBackgroundResource(R.mipmap.top_trink_red);
            }else {
                holder.tv_orderinfo_left.setText("出");
                holder.tv_orderinfo_left.setBackgroundResource(R.mipmap.top_trink_gray);
            }
        }else{    //是外卖
            holder.tv_orderinfo_left.setVisibility(View.GONE);
            holder.iv_orderinfo_left.setVisibility(View.VISIBLE);
            holder.tv_area1.setVisibility(View.INVISIBLE);
            holder.tv_area2.setVisibility(View.INVISIBLE);
            if("baidu".equals(list.get(i).getOrderfrom())){           //判断是那种类型的外卖,设置外卖的图标
                holder.iv_orderinfo_left.setImageResource(R.mipmap.waimai_baidu);   //百度
            }else if("meituan".equals(list.get(i).getOrderfrom())){
                holder.iv_orderinfo_left.setImageResource(R.mipmap.waimai_meituan);   //美团
            }else if("elme".equals(list.get(i).getOrderfrom())){
                holder.iv_orderinfo_left.setImageResource(R.mipmap.waimai_eleme);   //饿了么
            }
        }
        return view;
    }

    private class ViewHolder {
        CardView cardView;
        TextView tv_money, tv_time, tv_order, tv_number,tv_orderinfo_left,tv_area1,tv_area2;
        ImageView iv_orderinfo_left;

        public ViewHolder(View view) {
            cardView = (CardView) view.findViewById(R.id.cardView);
            tv_money = (TextView) view.findViewById(R.id.tv_money);     //钱
            tv_time = (TextView) view.findViewById(R.id.tv_time);        //时间
            tv_order = (TextView) view.findViewById(R.id.tv_order);    //订单编号
            tv_number = (TextView) view.findViewById(R.id.tv_number);
            tv_orderinfo_left = (TextView) view.findViewById(R.id.tv_orderinfo_left);  //待,堂吃才有
            iv_orderinfo_left = (ImageView) view.findViewById(R.id.iv_orderinfo_left);   //哪一家外卖
            tv_area1 = (TextView) view.findViewById(R.id.tv_area1);  //桌号1
            tv_area2 = (TextView) view.findViewById(R.id.tv_area2);  //桌号2


        }

        public void reset() {
            cardView.setCardBackgroundColor(Contants.getColor(context, R.color.white));
            tv_money.setTextColor(Contants.getColor(context, R.color.black));
            tv_time.setTextColor(Contants.getColor(context, R.color.black));
            tv_order.setTextColor(Contants.getColor(context, R.color.black));
            tv_number.setTextColor(Contants.getColor(context, R.color.black));
        }

        public void check() {
            cardView.setCardBackgroundColor(Contants.getColor(context, R.color.red));
            tv_money.setTextColor(Contants.getColor(context, R.color.white));
            tv_time.setTextColor(Contants.getColor(context, R.color.white));
            tv_order.setTextColor(Contants.getColor(context, R.color.white));
            tv_number.setTextColor(Contants.getColor(context, R.color.white));
        }
    }
}
